<template>
	<div class="system-company-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="800px" :close-on-click-modal="false">
			<el-form ref="companyDialogFormRef" :model="state.ruleForm" size="default" label-width="60px" label-position="top">
				<el-row :gutter="35">
					<el-col :xs="24" class="mb20">
						<el-form-item label="公告标题">
							<el-input v-model="state.ruleForm.title" placeholder="请输入公告标题" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="公告描述">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入公告描述" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="公告内容">
							<Editor v-model:get-html="state.ruleForm.content" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			
			<template #footer>
				<span class="dialog-footer">
					<el-button type="warning" @click="yulan" size="default">预 览</el-button>
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
		<el-dialog title="预览" v-model="state.yulanData.isShowDialog" width="800px" :close-on-click-modal="false">
			<div>
                <div class="juzhong">
                    <h2>{{state.ruleForm.title}}</h2>
                    <h5 style="margin-top:15px">{{dateFormatYMDHMS(new Date().getTime())}}</h5>
                </div>
                
                <div v-html="state.ruleForm.content" style="text-align:left;max-width:900px;margin:0 auto;margin-top:20px;"></div>
            </div>
		</el-dialog>
	</div>
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref, defineAsyncComponent } from 'vue';
// 引入组件
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { afficheApi } from '/@/api/affiche/index';

import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 定义变量内容
const companyDialogFormRef = ref();
const state = reactive({
	yulanData:{
		isShowDialog:false,
	},
	ruleForm: {
		title: '',
		remark: '',
		content: '',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改公告';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增公告';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			title: '',
			remark: '',
			content: '',
		}
	}
	state.dialog.isShowDialog = true;
};
const yulan = () => {
	state.yulanData.isShowDialog = true
}
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		afficheApi().add(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}else{
		afficheApi().edit(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
.juzhong{
    text-align: center;
    padding-bottom:15px;
    border-bottom:1px solid #444444;
}
:deep(.w-e-text-container p){
	margin: 0;
}
</style>
